package cn.edu.imnu.common.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;

/**
 * 文件相关的工具类
 * @author MCR
 *
 */
@Component("fileUtil")
public class FileUtil
{
	/**
	 * 文件拷贝
	 * @return
	 */
	public boolean copyFile(File souFile ,File tarFile)
	{
		if(!tarFile.getParentFile().exists())
		{
			tarFile.getParentFile().mkdirs() ; 
		}
		
		try
		{
			return this.copyFile(new FileInputStream(souFile),  new FileOutputStream(tarFile));
		} catch (FileNotFoundException e)
		{
			ConstatFinalUtil.LOGGER.error("文件拷贝关闭流失败了;原始文件:{},目标文件:{}," , souFile,tarFile,e);
		}
		return false ;
	}
	
	/**
	 * 文件拷贝
	 * @return
	 */
	public boolean copyFile(InputStream is ,OutputStream os)
	{
		BufferedInputStream bis = null ;
		BufferedOutputStream bos = null ; 
		try
		{
			bis = new BufferedInputStream(is);
			bos = new BufferedOutputStream(os);
			
			byte[] b = new byte[1024];
			int len = 0 ; 
			while((len = bis.read(b)) != -1)
			{
				bos.write(b, 0, len);
			}
			return true ; 
		} catch (IOException e)
		{
			ConstatFinalUtil.LOGGER.error("文件拷贝(流)失败了;",e);
		}finally 
		{
			try
			{
				if(bis != null)
				{
					bis.close();
					bis = null  ;
				}
			} catch (IOException e)
			{
				ConstatFinalUtil.LOGGER.error("文件拷贝(流)关闭流失败了;原始文件:{},目标文件:{},",e);
			}
			
			try
			{
				if(bos != null)
				{
					bos.close();
					bos = null ; 
				}
			} catch (IOException e)
			{
				ConstatFinalUtil.LOGGER.error("文件拷贝(流)关闭流失败了;原始文件:{},目标文件:{}," ,e);
			}
		}
		return false ;
	}
	
	/**
	 * 读取一个文件的内容
	 * @param sourceFile	原始文件内容
	 * @return	文件内容的字符串
	 */
	public String readFile(File sourceFile)
	{
		try
		{
			return this.readFile(new FileInputStream(sourceFile));
		} catch (FileNotFoundException e)
		{
			ConstatFinalUtil.LOGGER.error("文件不存在;文件路径:{}",sourceFile,e);
		}
		return "" ;
	}
	
	/**
	 * 读取一个文件内容,
	 * @param is	输入流
	 * @return
	 */
	public String readFile(InputStream is)
	{
		StringBuffer sb = new StringBuffer();
		BufferedReader br = null ;
		try
		{
			br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String line = "" ; 
			while((line = br.readLine()) != null)
			{
				//System.out.println(line);
				/* 处理注释 */
				line = line.trim() ;
				if("".equalsIgnoreCase(line) || line.startsWith("//"))
				{
					continue ; 
				}
				
				sb.append(line);
			}
		}catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.error("读取输入流内容失败了:",e);
		}finally
		{
			try
			{
				if(br != null)
				{
					br.close();
				}
			} catch (IOException e)
			{
				ConstatFinalUtil.LOGGER.error("读取输入流内容关闭时失败了:",e);
			}
		}
		return sb.toString() ;
	}
	
	public static void main(String[] args)
	{
		FileUtil fileUtil = new FileUtil();
		File sourceFile = new File("D:/workspaces/毕设/vacate/vacate-common/src/main/resources/resourceInfo.json");
		String res = fileUtil.readFile(sourceFile);
		System.out.println(res);
		/* 测试解析JSON */
		JSON.parse(res);
	}
}
